package com.cskaoyan.javase.exception._1handle._4throw;

/**
 * @description: 结合成员变量的封装和set方法，
 *          判断输入的参数是否合法（IllegalArgumentException）
 * @author: wuguidong@cskaoyan.onaliyun.com
 **/

public class Demo2 {
    public static void main(String[] args) {
        Student s = new Student();
        try {
            s.setAge(100);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }
        System.out.println(s.getAge());
    }
}

class Student {
    private int age;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        //设置年龄时,必须在1-126岁之间
        if (age > 126) throw new IllegalArgumentException("您修仙了吗?");
        if (age < 1) throw new IllegalArgumentException("您幸福吗?");
        this.age = age;
    }
}
